package com.xbongbong.paas.service.workflow.log.impl;

import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.service.workflow.log.WorkflowTaskLogItemService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.ProErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.workflow.domain.entity.WorkflowTaskLogEntity;
import com.xbongbong.workflow.domain.entity.WorkflowTaskLogItemEntity;
import com.xbongbong.workflow.enums.WorkflowNodeTypeEnum;
import com.xbongbong.workflow.model.WorkflowTaskLogItemModel;
import com.xbongbong.workflow.model.WorkflowTaskLogModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/06/17 15:45
 */
@Service("workflowTaskLogItemService")
public class WorkflowTaskLogItemServiceImpl implements WorkflowTaskLogItemService {

    @Resource
    private WorkflowTaskLogItemModel workflowTaskLogItemModel;
    @Resource
    private WorkflowTaskLogModel workflowTaskLogModel;


    @Override
    public WorkflowTaskLogItemEntity getLastApproveNodeByTaskId(String corpid, Long taskId) throws XbbException {

        WorkflowTaskLogEntity workflowTaskLogEntity = workflowTaskLogModel.getByTaskid(taskId, corpid);
        if (Objects.isNull(workflowTaskLogEntity)) {
            throw new XbbException(ProErrorCodeEnum.API_ERROR_260064);
        }
        Long taskLogId = workflowTaskLogEntity.getId();
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID, corpid);
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        param.put("workflowTaskLogId", taskLogId);
        param.put("orderByStr", " id desc ");
        param.put("pageSize", 3);
        List<WorkflowTaskLogItemEntity> list = workflowTaskLogItemModel.findEntitys(param);
        if (Objects.equals(list.size(), BasicConstant.THREE)) {
            WorkflowTaskLogItemEntity workflowTaskLogItemEntity = list.get(1);
            if (Objects.equals(workflowTaskLogItemEntity.getNodeType(), WorkflowNodeTypeEnum.APPROVE_NODE.getType())) {
                return workflowTaskLogItemEntity;
            } else if (Objects.equals(workflowTaskLogItemEntity.getNodeType(), WorkflowNodeTypeEnum.CONDITION_NODE.getType()) && Objects.equals(list.get(BasicConstant.TWO).getNodeType(), WorkflowNodeTypeEnum.APPROVE_NODE.getType())) {
                 // 如果中间的节点是条件节点，那么取第三个审批节点
                 return list.get(BasicConstant.TWO);
             }
        }else if (Objects.equals(list.size(), BasicConstant.TWO)) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.NOT_ALLOWED_TO_BACK);
        }
        throw new XbbException(ProErrorCodeEnum.API_ERROR_260064);
    }
}
